﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Windows.Threading;

namespace LetsGoToSchool
{
    /// <summary>
    /// Interaction logic for ImageFrame.xaml
    /// A wrapper around the image used in image manager 
    /// to show the border and provide delete function 
    /// </summary>
    public partial class ImageFrame : SurfaceUserControl
    {
        private Image _image;
        private ImageManager _manager;
        private bool _isDirectDown = false;
        private DispatcherTimer _timer = new DispatcherTimer();

        public ImageFrame(Image img, ImageManager manager)
        {
            InitializeComponent();
            _image = img;
            _manager = manager;
            _main.Children.Add(_image);
            _main.Width = _image.Width;
            _main.Height = _image.Height;

            _timer.Interval = TimeSpan.FromSeconds(1.5);
            _timer.Tick += ContactDownTimer_Tick;

            Contacts.AddPreviewContactDownHandler(this, ImageFrame_PreviewContactDown);
        }

        /// <summary>
        /// Remove the Image from Grid and Database
        /// </summary>
        /// <param name="sender"> object </param>
        /// <param name="e"> RoutedEventArgs </param>
        private void _deleteButton_Click(object sender, RoutedEventArgs e)
        {
            ((Grid)this.Parent).Children.Remove(this);
            Database.removeImage(((BitmapImage)_image.Source).UriSource.AbsolutePath);
        }

        private void ImageFrame_PreviewContactDown(object sender, ContactEventArgs e)
        {
            _timer.Stop();
            _manager.HideAllBorders();
            _isDirectDown = true;
            _border.Visibility = Visibility.Visible;
            _timer.Tag = e.Contact;
            _timer.Start();
        }

        /// <summary>
        /// show up the delete button only when one contact is held down 
        /// for 1.5 seconds and it's position hasn't change much
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ContactDownTimer_Tick(object sender, EventArgs e)
        {
            _timer.Stop();
            _isDirectDown = false;
            Dictionary<Contact, Point> trackedContacts = _manager.getTrackedContacts();
            if (trackedContacts.Count == 1)
            {
                IEnumerator<Contact> enumerator = trackedContacts.Keys.GetEnumerator();
                enumerator.MoveNext();
                Contact c = enumerator.Current;
                if (c == _timer.Tag && Utils.Distance2(c.GetPosition(_manager), trackedContacts[c]) <= 25)
                {
                    _deleteButton.Visibility = Visibility.Visible;
                    return;
                }
            }
            _border.Visibility = Visibility.Hidden;
        }

        public void hideFrame()
        {
            _deleteButton.Visibility = Visibility.Hidden;
            if (!_isDirectDown)
            {
                _border.Visibility = Visibility.Hidden;
            }
        }

        public void hideBorder()
        {
            _border.Visibility = Visibility.Hidden;
        }
    }
}
